package com.yuexun.dhdoctor.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import org.kymjs.kjframe.ui.ViewInject;

/**
 * Created by Administrator on 2017/3/14 0014.
 */

public class MediaPlayerUtils {
    private MediaPlayer player = null;
    private Handler handler = null;
    private static Context context;
    private static MediaPlayerUtils instance = null;
    private int playPosition;

    private MediaPlayerUtils() {
    }

    public static MediaPlayerUtils getInstance(Context context) {
        if (instance == null) {
            MediaPlayerUtils.context = context;
            instance = new MediaPlayerUtils();
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephonyManager.listen(new MyPhoneListener(),
                    PhoneStateListener.LISTEN_CALL_STATE);
        }
        return instance;
    }

    public void playVoice(final String voiceUrl, final Handler handler){
        this.handler = handler;
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            player.setDataSource(voiceUrl);
//            player.prepare();
            // 通过异步的方式装载媒体资源
            player.prepareAsync();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    // 装载完毕回调
//                    player.seekTo(0);
                    player.start();

                    Message msg = handler.obtainMessage();
                    msg.obj = player;
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
            });
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    stopPlay();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            ViewInject.toast("播放失败");
        }
    }

    public void stopPlay(){
        if (player != null) {
            if (player.isPlaying()) {
                player.stop();
            }
//            player.release();
            player = null;

            Message msg = handler.obtainMessage();
            msg.obj = player;
            msg.what = 1;
            handler.sendMessage(msg);
        }

        context = null;
        instance = null;
    }

    /**
     * 只有电话来了之后才暂停音乐的播放
     */
    static class MyPhoneListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:// 电话来了
                    if (instance!=null) {
                        instance.callIsComing();
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE: // 通话结束
                    if (instance!=null) {
                        instance.callIsDown();
                    }
                    break;
            }
        }
    }

    /**
     * 来电话了
     */
    public void callIsComing() {
        if (player.isPlaying()) {
            playPosition = player.getCurrentPosition();// 获得当前播放位置
            player.stop();
        }
    }

    /**
     * 通话结束
     */
    public void callIsDown() {
        if (playPosition > 0) {
            player.seekTo(playPosition);
            playPosition = 0;
        }
    }
}
